package com.example.reviewdemo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.reviewdemo.MainActivity;
import com.example.reviewdemo.R;
import com.example.reviewdemo.bean.User;
import com.example.reviewdemo.constant.Constants;
import com.example.reviewdemo.database.UserDatabase;
import com.example.reviewdemo.login.LoginActivity;
import com.example.reviewdemo.utils.DateUtil;
import com.example.reviewdemo.utils.PasswordUtil;
import com.example.reviewdemo.utils.ToastUtil;

import java.util.Date;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView et_username, et_password, et_confirm_password;
    private TextView go_login;

    private CheckBox ck_choose;
    private Button go_register;
    private UserDatabase userDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userDatabase = Room.databaseBuilder(this, UserDatabase.class, "user")
                .addMigrations()
                .allowMainThreadQueries()
                .build();
        initView();
    }

    private void initView() {
        et_username=findViewById(R.id.et_username);
        et_password=findViewById(R.id.et_password);
        et_confirm_password=findViewById(R.id.et_confirm_password);
        go_login=findViewById(R.id.go_login);
        ck_choose=findViewById(R.id.ck_choose);

        go_register=findViewById(R.id.go_register);
        go_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        String username = et_username.getText().toString();
        String password = et_password.getText().toString();
        String confirm_password = et_confirm_password.getText().toString();
        boolean checked = ck_choose.isChecked();
        User uername = userDatabase.userDao().findByUsername(username);

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirm_password)){
            ToastUtil.toastShort(this,Constants.FORM_NULL);
        } else if (!TextUtils.equals(password, confirm_password)){
            ToastUtil.toastShort(this,Constants.PASSWORD_CONFIRM_ERROR);
        } else if (uername!=null && uername.getUsername() != null){
            ToastUtil.toastShort(this,Constants.USERNAME_EXIST);
        } else if (checked) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(PasswordUtil.hashPassword(password));
            user.setCreateTime(DateUtil.format(new Date()));
            user.setUpdateTime(DateUtil.format(new Date()));
            userDatabase.userDao().insert(user);
            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
        } else {
            ToastUtil.toastShort(this,Constants.DISAGREE);
        }

    }
}